﻿using MediatR;
using S.RBAC.Domain.Help;
using S.RBAC.Domain.Model;
using S.RBAC.Infrastructure.Interface;
using S.RBAC.Read.Api.Application.Command.DepartmentCommand;
using System.Collections.Generic;

namespace S.RBAC.Read.Api.Application.CommandHandlers.DepartmentHandler
{
    public class DepartmentGetNameCommandHandler : IRequestHandler<DepartmentGetNameCommand, List<MenuItem>>
    {
        protected readonly IDepartmentRepository _department;

        public DepartmentGetNameCommandHandler(IDepartmentRepository department)
        {
            _department = department;
        }

        public async Task<List<MenuItem>> Handle(DepartmentGetNameCommand request, CancellationToken cancellationToken)
        {
            List<Department> departments = await _department.GetListDepartment(t => t.PId == request.PId);

            List<MenuItem> menuList = departments.Select(t => new MenuItem
            {
                Value = t.DepartId.ToString(),
                Label = t.DepartName,
                //子分类列表，根据Id得到子列表
                //Children = GetGoodsTypeList(gt.GTId).Count > 0 ? GetGoodsTypeList(gt.GTId) : null
            }).ToList();

            return menuList;
        }

        //public async Task<List<MenuItem>> Handle(DepartmentGetNameCommand request, CancellationToken cancellationToken)
        //{
        //    List<Department> departments = await _department.GetListDepartment(t => t.PId == request.PId);

        //    var dto = new MenuItem()
        //    {
        //        Value = 0.ToString(),
        //        Label = "宏光养羊",
        //        Children = new List<MenuItem>()
        //    };

        //    foreach (var item in departments)
        //    {
        //        dto.Children.Add(new MenuItem
        //        {
        //            Value = item.DepartId.ToString(),
        //            Label = item.DepartName,
        //            Children = dto.Children
        //        });
        //    }

        //    var result = new List<MenuItem>
        //    {
        //        dto
        //    };

        //    return result;
        //}
    }
}
